package com.atguigu.gmall.ums.service.impl;

import com.atguigu.gmall.common.utils.FormUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.ums.mapper.UserMapper;
import com.atguigu.gmall.ums.entity.UserEntity;
import com.atguigu.gmall.ums.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<UserEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<UserEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public Boolean check(String data, Integer type) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        //要校验的数据类型：1，用户名；2，手机；3，邮箱
        //根据data 到数据库user表中查询记录条数，结果==0 代表没有被占用  可用返回true
        switch (type){
            case 1:
                wrapper.eq(UserEntity::getUsername , data);
                break;
            case 2:
                wrapper.eq(UserEntity::getPhone , data);
                break;
            case 3:
                wrapper.eq(UserEntity::getEmail , data);
                break;
        }

        return this.count(wrapper)==0;
    }

    @Override
    public void register(UserEntity userEntity, String code) {
        //1、验证验证码:略

        //2、验证注册参数
        String username = userEntity.getUsername();
        String password = userEntity.getPassword();
        String phone = userEntity.getPhone();
        String email = userEntity.getEmail();
        if(StringUtils.isEmpty(username)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(phone)||
                StringUtils.isEmpty(email)||
                !FormUtils.isMobile(phone)
        ){
            throw new RuntimeException("注册参数错误");
        }
        //3、密码加密
        //生成盐
        String salt = UUID.randomUUID().toString().substring(0,8);
        //MD5加密相同的字符串加密后的结果一样 ，所以md5会结合加盐保证安全
        String endcodePwd = DigestUtils.md5Hex(DigestUtils.md5Hex(password)+salt);
        userEntity.setSalt(salt);
        userEntity.setPassword(endcodePwd);
        //初始化用户的默认值
        byte[] encode = Base64.getEncoder().encode(("谷粉" + UUID.randomUUID().toString().substring(0, 8)).getBytes());
        userEntity.setNickname(new String(encode));
        userEntity.setCreateTime(new Date());
        userEntity.setIntegration(0);
        userEntity.setGrowth(0);
        userEntity.setLevelId(1L);
        userEntity.setStatus(1);
//        userEntity.setHeader("");
        //4、保存到数据库
        this.save(userEntity);
    }

    @Override
    public UserEntity queryUser(String loginName, String password) {

        UserEntity userEntity = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUsername, loginName)
                .or()
                .eq(UserEntity::getPhone, loginName)
                .or()
                .eq(UserEntity::getEmail, loginName));
        if(userEntity==null){
            return null;//账号不存在
        }
        //账号存在 验证密码
        String encodedPwd = userEntity.getPassword();

        boolean equals = StringUtils.equals(DigestUtils.md5Hex(DigestUtils.md5Hex(password) + userEntity.getSalt()),
                encodedPwd);
        if(!equals){
            return null;//密码错误
        }
        userEntity.setPassword("PASSWORD IS PRIVATE");
        byte[] decode = Base64.getDecoder().decode(userEntity.getNickname().getBytes());
        userEntity.setNickname(new String(decode));
        return userEntity;
    }

}